package com.example.cheeseerpadmin.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.cheeseerpadmin.bus.entity.BusGoods;
import com.example.cheeseerpadmin.bus.entity.BusOutport;
import com.example.cheeseerpadmin.bus.entity.BusProvider;
import com.example.cheeseerpadmin.bus.service.BusGoodsService;
import com.example.cheeseerpadmin.bus.service.BusOutportService;
import com.example.cheeseerpadmin.bus.service.BusProviderService;
import com.example.cheeseerpadmin.bus.vo.OutportVo;
import com.example.cheeseerpadmin.sys.common.DataGridView;
import com.example.cheeseerpadmin.sys.common.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cheese
 * @since 2022-02-17
 */
@RestController
@RequestMapping("/outport")
public class OutportController {
    @Autowired
    public BusOutportService busOutportService;
    @Autowired
    public BusProviderService busProviderService;
    @Autowired
    public BusGoodsService busGoodsService;

    /**
     * 增加退货记录
     *
     * @param id
     * @param number
     * @param remark
     * @return
     */
    @RequestMapping("/addOutport")
    public ResultObj addOutport(Integer id, Integer number, String remark) {
        try {
            busOutportService.addOutport(id, number, remark);
            return ResultObj.OPERATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.OPERATE_ERROR;
        }
    }

    /**
     * 加载全部的退货信息
     * @param outportVo
     * @return
     */
    @RequestMapping("/loadAllOutport")
    public DataGridView loadAllOutport(OutportVo outportVo) {
        Page<BusOutport> busOutportPage = new Page<>();
        QueryWrapper<BusOutport> busOutportQueryWrapper = new QueryWrapper<>();
        busOutportQueryWrapper.eq(outportVo.getProviderid() != null && outportVo.getProviderid() != 0, "providerid", outportVo.getProviderid());
        busOutportQueryWrapper.eq(outportVo.getGoodsid() != null && outportVo.getGoodsid() != 0, "goodsid", outportVo.getGoodsid());
        busOutportQueryWrapper.ge(outportVo.getStartDate() != null, "outputtime", outportVo.getStartDate());
        busOutportQueryWrapper.le(outportVo.getEndDate() != null, "outputtime", outportVo.getEndDate());
        busOutportQueryWrapper.like(StringUtils.isNotBlank(outportVo.getOperateperson()), "operateperson", outportVo.getOperateperson());
        busOutportQueryWrapper.like(StringUtils.isNotBlank(outportVo.getRemark()), "remark", outportVo.getRemark());
        busOutportQueryWrapper.orderByDesc("outputtime");
        Page<BusOutport> page = busOutportService.page(busOutportPage, busOutportQueryWrapper);
        List<BusOutport> list = page.getRecords();
        for (BusOutport busOutport : list) {
            Integer providerid = busOutport.getProviderid();
            BusProvider busProvider = busProviderService.getById(providerid);
            if (busProvider != null) {
                busOutport.setProvidername(busProvider.getProvidername());
            }
            Integer goodsid = busOutport.getGoodsid();
            BusGoods busGoods = busGoodsService.getById(goodsid);
            if (busGoods != null) {
                busOutport.setGoodsname(busGoods.getGoodsname());
                busOutport.setSize(busGoods.getSize());
            }
        }
        return new DataGridView((long) list.size(), list);
    }
}

